#define  _CRT_SECURE_NO_WARNINGS 1
#include "List.h"

static ListNode* BuyListNode(ListDataType data)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		return NULL;
	}
	newnode->_data = data;
	newnode->_next = NULL;
	newnode->_prev = NULL;
	return newnode;
}


ListNode* ListInit()
{
	ListNode* head = BuyListNode(0);
	head->_next = head;
	head->_prev = head;
	return head;
}
void ListPushBack(ListNode* ps, ListDataType data)
{
	ListInsert(ps, data);
}
void ListPushFront(ListNode* ps, ListDataType data)
{
	ListInsert(ps->_next, data);
}
void ListPopBack(ListNode* ps)
{
	ListErase(ps->_prev);
}
void ListPopFront(ListNode* ps)
{
	ListErase(ps->_next);
}
void ListInsert(ListNode* pos, ListDataType data)
{
	ListNode* prev = pos->_prev;
	ListNode* newnode = BuyListNode(data);
	prev->_next = newnode;
	newnode->_prev = prev;
	newnode->_next = pos;
	pos->_prev = newnode;
}
void ListErase(ListNode* pos)
{
	assert(pos != pos->_next);
	ListNode* prev = pos->_prev, * next = pos->_next;
	prev->_next = next;
	next->_prev = prev;
	free(pos);
}
void ListPrint(ListNode* ps)
{
	ListNode* curr = ps->_next;
	while (curr != ps)
	{
		printf("%d  <-> ", curr->_data);
		curr = curr->_next;
	}
	printf("NULL\n");
}
ListNode* ListFind(ListNode* ps, ListDataType data)
{
	ListNode* curr = ps->_next;
	while (curr != ps)
	{
		if (curr->_data == data)
			return curr;
		curr = curr->_next;
	}
	return NULL;
}
